<?php

namespace app\api\model;

use think\Model;

class Sorting extends Model {

	//删除分类
	public function sort_delete($sid) {
		$res = $this->field('id,image')->where(['id' => $sid])->find();
		$auth = new \Qiniu\Auth(C('AccessKey'), C('SecretKey'));
		$bucketMgr = new \Qiniu\Storage\BucketManager($auth);
		$res_tmp = $bucketMgr->delete(C('bucket'), substr($res['image'], strlen(C('qn_url'))));
		return $this->delete($sid);
	}

	//查询分类下级
	public function get_junior($sid) {
		$res = $this->field('id')->where(['pid' => $sid])->select();
		foreach ($res as $v) {
			$this->sort_delete($v['id']);
		}
		return $this->sort_delete($sid);
	}

	/**
	 * 分类整合
	 */
	public function typeArray($array) {
		$con = null;
		$na = [];
		foreach ($array as $k => $v) {
			$na[$v['id']] = $v['pid'] ? $na[$v['pid']] . $v['id'] . '|' : $v['id'] . '|';
			$an[$v['id']] = $k;
		}

		if (empty($na)) {
			return $na;
		}
		asort($na); //排序
		foreach ($na as $k => $v) {
			$s = substr_count($v, "|");
			$con[$k] = str_repeat("<span style='margin-left: 15px'>&nbsp;&nbsp;├&nbsp;</span>", ($s - 1)) . $array[$an[$k]]['sname'];
		}
		$res_list = [];
		foreach ($con as $k => $v) {
			foreach ($array as $key => $value) {
				if ($k == $value['id']) {
					$value['show'] = $v;
					$res_list[] = $value;
					unset($array[$key]);
				}
			}
		}
		return $res_list;
	}

}
